//-----------------Sonic CD Antlion Script-------------------//
// Yeah...

// Aliases
#alias Object.Value0		:	Object.Timer
#alias Object.Value1		:	Object.Timer2
#alias Object.Value2		:	Object.Timer3
#alias Object.Value3		:	Object.IYpos
//Object.Frame
#alias Object.Value4		:	Object.EndYpos
#alias Object.Value6		:	Object.YVelocity

#alias Object.PropertyValue	:	Object.Quality


// States
#alias 0	:	Antlion_Idle
#alias 1	:	Antlion_MoveUp
#alias 2	:	ANTLION_ATTACK1
#alias 3	:	ANTLION_ATTACK2
#alias 4	:	ANTLION_DOWN

#alias 0	:	INK_NONE
#alias 1	:	INK_BLEND
#alias 2	:	INK_ALPHA
#alias 3	:	INK_ADD
#alias 4	:	INK_SUB

//140

// Badnik Quality / Property Values
#alias 0	:	GOOD_QUALITY
#alias 1	:	BAD_QUALITY

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

sub ObjectMain
	CallFunction(StageSetup_CheckGoodFuture)	// Check if this should be a flower
	switch Object.State
	case Antlion_Idle
		if Object.PropertyValue == GOOD_QUALITY
			Object.YVelocity = -360000
		else
			Object.YVelocity = -50000
		end if
		break

	case Antlion_MoveUp
		if Object.PropertyValue == GOOD_QUALITY
			Object.YVelocity -= 40000
			Object.YPos += Object.YVelocity
			if Object.YVelocity < -880001
				Object.State = ANTLION_ATTACK1
				//Object.YPos -= 50000
			end if
		else
			Object.YVelocity -= 4000
			Object.YPos += Object.YVelocity
			if Object.YVelocity < -270001
				Object.State = ANTLION_ATTACK1
				//Object.YPos -= 50000
			end if
		end if
		break

	case ANTLION_ATTACK1
		Object.Timer++
		if Object.PropertyValue == GOOD_QUALITY
			if Object.Timer >= 4
				Object.Timer = 0
				if Object.Frame < 2
					Object.Frame++
				else
					Object.Timer = 0
					Object.State = ANTLION_ATTACK2
				end if
			end if
		else
			if Object.Timer >= 8
				Object.Timer = 0
				if Object.Frame < 2
					Object.Frame++
				else
					Object.Timer = 0
					Object.State = ANTLION_ATTACK2
				end if
			end if
		end if
		break

	case ANTLION_ATTACK2
		if Object.PropertyValue == GOOD_QUALITY
			Object.YVelocity = 350000
		else
			Object.YVelocity = 50000
		end if
		break

	case ANTLION_DOWN
		if Object.ypos > Object.IYpos
			Object.YPos = Object.IYpos
			Object.State = Antlion_Idle
		else
			if Object.PropertyValue == GOOD_QUALITY
				Object.YVelocity += 40000
				Object.YPos += Object.YVelocity
			else
				Object.YVelocity += 4000
				Object.YPos += Object.YVelocity
			end if
		end if
		break
			
	end switch
	
	// Reset object when out of bounds
	if Object.OutOfBounds == true
		Object.Priority = PRIORITY_BOUNDS
		Object.State = Antlion_Idle
		Object.Timer = 0
		Object.YPos = Object.IYpos
	end if

	//CallFunction(StageSetup_CheckGoodFuture)	// Check if it should be a flower
end sub


sub ObjectPlayerInteraction
	switch Object.State
	case Antlion_Idle
		PlayerObjectCollision(C_TOUCH, -72, -200, 72, 0) //Detect
		if CheckResult == true
			Object.State = Antlion_MoveUp
		end if
		break

	case ANTLION_ATTACK2
		PlayerObjectCollision(C_TOUCH, -16, -142, 16, -13)
		if CheckResult == true
			if Object.Frame > 0
				Object.Frame--
				PlayStageSFX(4, false)
			else
				Object.Timer3 = 0
			end if
		else
			Object.Timer3++
			if Object.PropertyValue == GOOD_QUALITY
				if Object.Timer3 == 48
					Object.Timer3 = 0
					if Object.Frame < 2
						Object.Frame++
					end if
				end if
			else
				if Object.Timer3 == 96
					Object.Timer3 = 0
					if Object.Frame < 2
						Object.Frame++
					end if
				end if
			end if
		end if

		PlayerObjectCollision(C_TOUCH, -72, -64, 72, 0) //Detect
		if CheckResult == false
			Object.Timer++
			if Object.Timer >= 120
				Object.Timer2++
				if Object.PropertyValue == GOOD_QUALITY
					if Object.Timer2 == 4
						Object.Timer2 = 0
						if Object.Frame > 0
							Object.Frame--
						else
							Object.State = ANTLION_DOWN
							Object.Frame = 0
						end if
					end if
				else
					if Object.Timer2 == 8
						Object.Timer2 = 0
						if Object.Frame > 0
							Object.Frame--
						else
							Object.State = ANTLION_DOWN
							Object.Frame = 0
						end if
					end if
				end if
			end if
		else
			Object.Timer = 0
		end if

		break
	end switch

	switch Object.Frame
	case 0
		PlayerObjectCollision(C_TOUCH, -16, -8, 16, 23)
		if CheckResult == true
			CallFunction(Player_BadnikBreak)
		end if

		if Object.State == ANTLION_ATTACK2
			PlayerObjectCollision(C_TOUCH, -8, -24, 8, -23)
			if CheckResult == true
				CallFunction(Player_Hit)
			end if
		end if
		break

	case 1
		PlayerObjectCollision(C_TOUCH, -16, -8, 16, 23)
		if CheckResult == true
			CallFunction(Player_BadnikBreak)
		end if

		PlayerObjectCollision(C_TOUCH, -8, -24, 8, -8)
		if CheckResult == true
			CallFunction(Player_Hit)
		end if
		break

	case 2
		PlayerObjectCollision(C_TOUCH, -16, -1, 16, 23)
		if CheckResult == true
			CallFunction(Player_BadnikBreak)
		end if
		
		PlayerObjectCollision(C_TOUCH, -8, -13, 8, -1)// Spike Touch
		if CheckResult == true
			CallFunction(Player_Hit)
		end if
		break

	end switch
end sub


sub ObjectDraw
	DrawSprite(Object.Frame)
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects.gif")

	SpriteFrame(-16, -23, 32, 46, 149, 83)			// #0 - Antlion Frame 1 (pincers closed)
	SpriteFrame(-16, -23, 32, 46, 116, 83)			// #1 - Antlion Frame 2 (pincers partially Open)
	SpriteFrame(-24, -23, 48, 46, 67, 83)			// #2 - Antlion Frame 3 (pincers fully Open)
	
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Antlion]
			Object[ArrayPos0].IYpos = Object[ArrayPos0].YPos
		end if
		ArrayPos0++
	loop
end sub

// ========================
// Editor Subs
// ========================

sub RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
		case 0 // Sprite
			checkResult = object.propertyValue
			break
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
		case 0 // Sprite
			object.propertyValue = editor.variableValue
			break
		end switch
	end if
end sub

sub RSDKDraw	
	if editor.showGizmos == true
		editor.drawingOverlay = true

		TempValue0 = object.ypos
		TempValue0 -= 0x880000
		DrawArrow(object.xpos, object.ypos, object.xpos, TempValue0, 255, 255, 255)
		
		object.inkEffect = INK_BLEND
		DrawSpriteFX(0, FX_INK, object.xpos, TempValue0)
		
		editor.drawingOverlay = false
	end if
	
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("R2/Objects.gif")
	SpriteFrame(-16, -23, 32, 46, 149, 83)			// #0 - Antlion Frame 1

	SetVariableAlias(ALIAS_VAR_PROPVAL, "quality")
	
	AddEditorVariable("quality")
	SetActiveVariable("quality")

	AddEnumVariable("Good Quality", 0)
	AddEnumVariable("Bad Quality", 1)
end sub